import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static int longestOnes(int[] nums, int k) {
        int ans = Integer.MIN_VALUE;
        int len = nums.length;
        int count = k;
        for(int left = 0, right = 0; right < len; right++) {
            //进窗口
            if(nums[right] == 0 && count != 0) {
                count--;
            }

            //出窗口
            if(nums[right] == 0 && count == 0) {
                while(nums[left] != 0) {
                    left++;
                }
                left++;
                count = 1;
            }

            //更新数据
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }

    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        Map<String,Integer> mapW = new HashMap<>();
        for(String x : words) {
            mapW.put(x,mapW.getOrDefault(x,0) + 1);
        }

        int lenS = s.length();
        int len = words[0].length();
        Map<String,Integer> mapS = new HashMap<>();
        int count = 0;
        int size = words.length;
        for(int i = 0; i < len; i++) {
            mapS.clear();
            for(int left = i, right = i + len; right <= lenS; right+=len) {
                String str = s.substring(right-len,right);
                mapS.put(str,mapS.getOrDefault(str,0) + 1); //进窗口

                if(mapS.get(str) <= mapW.getOrDefault(str,0)) {
                    count++;
                }

                //出窗口
                if(right - left > len * size) {
                    String ch = s.substring(left,left+len);
                    if(mapS.get(ch) <= mapW.getOrDefault(ch,0)) {
                        count--;
                    }
                    mapS.put(ch,mapS.get(ch)-1);
                    left += len;
                }

                //更新数据
                if(count == size) {
                    list.add(left);
                }
            }
        }

        return list;
    }

    public static String minWindow(String ss, String tt) {
        String str = "";
        if(ss.length() < tt.length()) {
            return str;
        }
        int lenS = Integer.MAX_VALUE;
        int[] hash1 = new int[128];
        int[] hash2 = new int[128];
        char[] s = ss.toCharArray();
        char[] t = tt.toCharArray();
        for(char x : t) {
            hash2[x]++;
        }

        int len = s.length;
        int count = 0;
        int size = t.length;
        for(int left = 0, right = 0; right < len; right++) {
            //进窗口
            char ch = s[right];
            hash1[ch]++;
            if(hash1[ch] <= hash2[ch]) {
                count++;
            }

            if(count == size) {
                //更新数据
                if(lenS > right - left + 1) {
                    lenS = right - left + 1;
                    str = ss.substring(left,right+1);
                }

                //出窗口
                char x = s[left++];
                hash1[x]--;
                count--;
                while(hash1[s[left]]-- > hash2[s[left]]) {
                    left++;
                }
            }
        }
        return str;
    }

    public static void main(String[] args) {
        /*int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
        int K = 2;
        longestOnes(nums,K);
        Map<Integer,Integer> mapt = new HashMap<>();*/

        /*String s ="wordgoodgoodgoodbestword";
        String[] words = {"word","good","best","good"};
        findSubstring(s,words);*/


        String s = "A";
        String t = "AA";
        System.out.println(minWindow(s, t));
    }
}
